var t = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
    return typeof t
  } :
  function(t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t
  },
  e = getApp(),
  a = e.requirejs("core"),
  r = e.requirejs("check");
Page({
  data: {
    shows: false,
    icons: e.requirejs("icons"),
    list: {},
    cart_list: [],
    data: [],
    showPicker: false,
    pvalOld: [0, 0, 0],
    pval: [0, 0, 0],
    areas: [],
    noArea: true,
    submit: true,
    coupon_display: false,
    coupon_num: 0,
    send_type: 'express',
    is_mention: 1, //1邮寄，2自提，3自选
    date: '',
    time: '17:00',
    formid: '',
    button_color: e.config.button_color,
    tearedbag:'',
    inum:1,
    list_chg:0,
    dikou_price:0,
    cha_redbag:0,
  },
  /**
   * 自提、邮寄
   */
  dispatchtype: function(e) {
    var that = this,
      data = a.pdata(e);
    that.setData(data);

    if (that.data.options.type && that.data.options.type == "cart") {
      that.cart_caculate();
    } else {
      that.caculate_fee(that.data.options.goods_id, that.data.options.total, function(fee) {
        that.caculate(fee)
      })
    }

  },
  share: function (e) {
    var that = this.data;
    var i = that.inum
    
    // console.log(that.list_chg)  //5
    if (i % 2 == 0) {
      i= i+1
      this.setData({
        inum: i,
        shows: false,
        "dikou_price": 0,
        'cha_redbag':0
      })
    } else {
      i= i+1
      this.setData({
        inum: i,
        shows: true,
        "dikou_price": that.list_chg
        // that.list_chg
      })
    }
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var get_time = a.time_str(e.detail.value + ' ' + this.data.time);
    var now_time = Date.parse(new Date()) / 1000;

    if (now_time > get_time) {
      a.error('不小于当前时间');
      return;
    }
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  onLoad: function(t) {
    a.setting();
    var i = this,
      data = this.data,
      d = {};
    this.setData({
      inum: 1
    })
    
    this.setData({
      options: t,
      button_color: getApp().config.button_color,
      font_color: getApp().config.font_color,
      config: getApp().config
    })
    a.get("user/About", {
      user_id: getApp().getCache("userinfo").uid
    }, function(t) {
      if (t.code == 0) {
        var send_type = i.data.send_type;
        if (t.info.is_mention == 2 || t.info.is_mention == 6) {
          send_type = 'selftake';
        } else if (t.info.is_mention == 4) {
          send_type = 'endpay';
        }
        i.setData({
          user_level: t.info.user_level,
          user_rebate: t.info.user_rebate,
          is_mention: t.info.is_mention,
          send_type: send_type
        })
      }
    })
    //购物车下单时获取订单信息
    if (i.data.options.type && i.data.options.type == "cart") {
      a.get("goods/CartGoods", i.data.options, function(t) {
        console.log(t)
        0 == t.code ? (
          i.setData({
            cart_list: t.info.info,
            "list.address": t.info.address,
            "list.activity": t.info.activity,
            show: true,
          }),
          i.cart_caculate()
        ) : a.alert(t.msg)
      })
    } else {
      //从商品详情下单时获取订单信息
      a.get("goods/GetGoods", i.data.options, function(t) {
        console.log(t)
        i.setData({
          list_chg:t.info.goods.dikou_price
        })
    console.log(i.data.list_chg)
        0 == t.code ? (
          i.setData({
            list: t.info,
            show: true,
          }),
          i.setData({
            tearedbag: t.tearedbag
          }),
          i.caculate_fee(i.data.options.goods_id, i.data.options.total, function(fee) {
            // console.log("fee",fee)
            i.caculate(fee)
          })
        ) : a.alert(t.msg)
      })
    }
  },
  onShow: function() {
    var i = this,
      d = e.getCache("orderAddress");
    if (d) {
      this.setData({
        "list.address": d,
      })
      if (!i.data.options.cart_id) {
        i.caculate_fee(i.data.options.goods_id, i.data.options.total, function(fee) {
          i.caculate(fee)
        })
      } else {
        i.cart_caculate()
      }
    };
  },
  CouponApi: function(money) {
    var that = this,
      coupon = [];
    a.get('Market/UserCoupon', {
        user_id: getApp().getCache("userinfo").uid
      },
      function(v) {
        if (v.code == 0) {
          if (v.info.length > 0) {
            var now_time = Date.parse(new Date()) / 1000;
            for (var i = 0; i < v.info.length; i++) {
              if (money >= v.info[i].satisfy_money && v.info[i].status == 0 && v.info[i].end_time >= now_time) {
                coupon.push(v.info[i])
              }
            }
            coupon.sort(function(x, y) {
              return parseFloat(y.satisfy_money) - parseFloat(x.satisfy_money);
            }); //按satisfy_money排序
            that.setData({
              coupon: coupon,
              coupon_num: coupon.length
            })
          }
        }
      })
  },
  select: function(e) {
    var that = this,
      money = that.data.data.order_money,
      discount_money = that.data.discount_money,
      vip_money = that.data.data.rebate_money,
      s = a.pdata(e);
    // console.log("coup",s);
    if (s.together == 1) {
      that.setData({
        "data.discount_money": 0.00
      })
      discount_money = 0
    } else {
      if (!discount_money) {
        discount_money = 0
      }
      that.setData({
        "data.discount_money": discount_money
      })

    }
    var pay_money = parseFloat(money) - parseFloat(discount_money) - parseFloat(vip_money) - parseFloat(s.coupon_money) + parseFloat(that.data.data.shipping_money);
    pay_money = parseFloat(pay_money).toFixed(2);
    if (pay_money <= 0) {
      pay_money = 0;
    }

    that.setData({
      "data.coupon_id": s.id,
      "data.coupon_money": s.coupon_money,
      "data.pay_money": pay_money,
      coupon_display: false
    })
  },
  open_coupon: function() {
    var display = this.data.coupon_display ? false : true;
    this.setData({
      coupon_display: display
    })
  },
  /**
   *返回触发事件
   * @param t 事件
   * @return array
   */
  toggle: function(t) {
    var e = a.pdata(t),
      i = e.id,
      d = e.type,
      r = {};
    r[d] = 0 == i || void 0 === i ? 1 : 0,
      this.setData(r)
  },
  /**
   *打电话
   */
  phone: function(t) {
    a.phone(t)
  },
  /**
   *单商品订单数量加减
   * @param t 点击事件
   * @return array
   */
  number: function(t) {
    var e = this,
      total = '',
      d = a.data(t).action;
    if (d == 'minus') {
      total = parseInt(e.data.options.total) - 1;
    } else if (d == 'plus') {
      total = parseInt(e.data.options.total) + 1;
    }
    e.setData({
        //list: c
        'options.total': total

      }),
      this.caculate()
  },

  /**
   *购物车订单数量加减
   * @param t 点击事件
   * @return array
   */
  cart_number: function(t) {
    var e = this,
      id = a.pdata(t).id,
      key = a.pdata(t).key,
      num = e.data.cart_list,
      d = a.data(t).action;
    if (d == 'minus') {
      num[key].num = parseInt(num[key].num) - 1;
    } else if (d == 'plus') {
      num[key].num = parseInt(num[key].num) + 1;
    }
    e.setData({
        'cart_list': num
      }),
      this.cart_caculate()
  },

  //运费
  caculate_fee: function(goods_id, num, obj) {
    var that = this,
        data = that.data.list.address;
    if (that.data.send_type == "selftake") {
      typeof obj == "function" && obj(0);
    } else {
      if (data.area && data.area > 0) {
        a.get('Market/UserFee', {
          goods_id: goods_id,
          num: num,
          area_id: data.area
        }, function(t) {
          if (t.code == 0) {
            typeof obj == "function" && obj(t.info);
          } else {
            a.alert(t.msg, function() {
              typeof obj == "function" && obj(0);
            });
          }
        })
      } else {
        typeof obj == "function" && obj(0);
      }
    }

  },

  //计算
  caculate: function(fee) {
    var e = this,
      discount_money = 0,
      activity = e.data.list.activity,
      a = e.data.list.address;
    //订单价 合计
    a.order_money = parseFloat(this.data.options.total).toFixed(2) * parseFloat(this.data.list.price).toFixed(2);
    //优惠券
    e.CouponApi(a.order_money);

    //减免活动
    for (var i = 0; i < activity.length; i++) {
      if (parseFloat(a.order_money) >= parseFloat(activity[i].satisfy_money)) {

        discount_money = activity[i].discount_money;
        a.discount_money = activity[i].discount_money;
        a.discount_id = activity[i].id;
        break;
      }
    }

    //会员折扣
    var rebate_money = 0.00;
    if (e.data.user_level && e.data.user_level != 0 && e.data.user_rebate && e.data.user_rebate < 10.00) {
      rebate_money = parseFloat(a.order_money) * (1 - parseFloat(e.data.user_rebate) / 10);
      rebate_money = rebate_money.toFixed(2);

    }
    a.rebate_money = rebate_money;
    //运费
    if (e.data.send_type == 'express' || e.data.send_type == "endpay") {
      a.shipping_money = fee;
      /*if (e.data.list.post_fee != null && e.data.list.post_fee != undefined) {
        var post_fee = e.data.list.post_fee;
        var bynum_snum = parseInt(post_fee.bynum_snum);
        var more = bynum_snum > 0 ? Math.ceil((parseInt(e.data.options.total) - parseInt(post_fee.bynum_snum)) / parseInt(post_fee.bynum_xnum)) : 0;
        var more_price = more > 0 ? more * parseFloat(post_fee.bynum_xprice) : 0;
        a.shipping_money = parseFloat(post_fee.bynum_sprice) + parseFloat(more_price);
      } else {
        a.shipping_money = 0;
      }*/

    } else {
      a.shipping_money = 0;
    }
    var tea_red = 0;
    //实付价
    a.pay_money = parseFloat(parseInt(e.data.options.total) * parseFloat(e.data.list.price) + a.shipping_money - parseFloat(discount_money) - parseFloat(rebate_money) - parseFloat(tea_red)).toFixed(2);
    
    if (parseFloat(a.pay_money) < 0) {
      a.pay_money = 0.00;
    }
    a.sku_id = e.data.options.sku_id + ":" + e.data.options.total;
    a.user_name = getApp().getCache("userinfo").nickName;
    a.buyer_id = getApp().getCache("userinfo").uid;
    a.cha_redbag = getApp().getCache("userinfo").cha_redbag;
    e.setData({
      data: a,
      discount_money: discount_money ? discount_money : 0,
      rebate_money: parseFloat(rebate_money)
    })
  },
  //购物车订单计算
  cart_caculate: function() {
    var e = this,
      discount_money = 0,
      activity = e.data.list.activity,
      total = 0,
      c = this.data.options,
      d = this.data.cart_list,
      a = e.data.list.address,
      order_money = 0,
      shipping_money = 0,
      pay_money = 0,
      fee_arr = [],
      sku_id = '';
    d.forEach(function(t) {
      //订单价
      order_money += parseFloat(t.num * t.price.price);
      //运费
      if (t.goods.shipping_fee_id) {
        if (fee_arr[t.goods.shipping_fee_id]) {
          fee_arr[t.goods.shipping_fee_id].num = parseInt(t.num) + parseInt(fee_arr[t.goods.shipping_fee_id].num);
        } else {
          fee_arr[t.goods.shipping_fee_id] = {
            'num': t.num,
            'goods_id': t.goods_id
          };
        }
      }
      /*if (t.post_fee != null && t.post_fee != undefined && e.data.send_type != "selftake") {
        var post_fee = t.post_fee;
        var bynum_snum = parseInt(post_fee.bynum_snum);
        var more = bynum_snum > 0 ? Math.ceil((parseInt(t.num) - parseInt(post_fee.bynum_snum)) / parseInt(post_fee.bynum_xnum)) : 0;
        var more_price = more > 0 ? more * parseFloat(post_fee.bynum_xprice) : 0;
        shipping_money += parseFloat(post_fee.bynum_sprice) + parseFloat(more_price);
      }*/
      sku_id += t.sku_id + ":" + t.num + ",";
      total += parseInt(t.num);
    })
    //优惠券
    e.CouponApi(order_money);
    //减免活动
    for (var i = 0; i < activity.length; i++) {
      if (order_money >= activity[i].satisfy_money) {
        discount_money = activity[i].discount_money;
        a.discount_money = activity[i].discount_money;
        a.discount_id = activity[i].id;
        break;
      }
    }
    //会员折扣
    var rebate_money = 0.00;
    if (e.data.user_level && e.data.user_level != 0 && e.data.user_rebate && e.data.user_rebate < 10.00) {
      // console.log(1 - parseFloat(e.data.user_rebate) / 10);
      rebate_money = parseFloat(order_money) * (1 - parseFloat(e.data.user_rebate) / 10);

      rebate_money = rebate_money.toFixed(2);
    }
    a.rebate_money = rebate_money;
    pay_money = parseFloat(order_money + shipping_money - discount_money - rebate_money).toFixed(2);
    a.order_money = parseFloat(order_money).toFixed(2);
    if (e.data.send_type == 'express' || e.data.send_type == 'endpay') {
      a.shipping_money = shipping_money;
    } else {
      a.shipping_money = 0;
    }
    if (parseFloat(pay_money) < 0) {
      pay_money = 0.00;
    }
    a.order_money = parseFloat(order_money).toFixed(2);
    a.pay_money = parseFloat(pay_money);
    a.sku_id = sku_id;
    a.user_name = getApp().getCache("userinfo").nickName;
    a.buyer_id = getApp().getCache("userinfo").uid;
    a.cha_redbag = list.goods.dikou_price;

    if (e.data.send_type != 'selftake' && fee_arr.length > 0) {
      fee_arr.forEach(function(s) {
        // console.log(s)
        e.caculate_fee(s.goods_id, s.num, function(fee) {
          //运费
          shipping_money += parseFloat(fee);
          a.shipping_money = shipping_money;

          a.pay_money += shipping_money;
          a.pay_money = parseFloat(a.pay_money).toFixed(2);
          e.setData({
            data: a,
            discount_money: discount_money ? discount_money : 0,
            rebate_money: rebate_money
          })
        })
      });
    } else {
      a.shipping_money = 0;
      e.setData({
        data: a,
        discount_money: discount_money ? discount_money : 0,
        rebate_money: rebate_money
      })
    }

    //求出商品总数量
    c.total = total;
    e.setData({
      options: c,
    })
  },
  //提交订单
  submit: function(k) {
    var e = this,
        t = this.data;
    e.setData({
      formid: k.detail.formId
    })
    t.data.cha_redbag = t.dikou_price
    // console.log(t.data.cha_redbag)

    // if (t.disabled) {
    //   return false;
    // }

    // 0元可支付
    // if (t.data.pay_money <= 0 && t.data.coupon_id == undefined && t.data.discount_id == undefined) {
    //   a.error('支付金额错误！');
    //   return false;
    // }

    if (t.send_type == 'selftake') {
      if (!t.data.consigner) {
        a.error('请填写姓名');
        return false;
      }
      if (!t.data.phone) {
        a.error('请填写电话');
        return false;
      }
      if (!t.date) {
        a.error('请选择自提日期');
        return false;
      }
      t.data.mention_time = a.time_str(t.date + ' ' + t.time);
      t.data.mailing_type = 2;
    } else if (t.send_type == 'endpay') {
        if (!t.list.address.phone || t.list.address.province == '') {
          a.error('请选择收货地址！');
          return false;
        }
        t.data.mailing_type = 4;
      } else {
        if (!t.list.address.phone || t.list.address.province == '') {
          a.error('请选择收货地址！');
          return false;
        }
        t.data.mailing_type = 1;
      }
    t.data.staff_id = getApp().globalData.card_id;

    e.setData({
      disabled: true
    })
    wx.showLoading({
      title: '请求中...',
    })
    a.get("Order/CreateOrder", t.data, function(t) {
        if (0 == t.code) {
          // console.log(t.data)
          e.setData({
            
            order_id: t.info,
            disabled: false
          })

          // 订单金额为0时调用接口，支付成功
          if (e.data.data.pay_money == 0) {
            a.get("pay/updPayStatus", {
              order_id: t.info,
              pay_type: 1
            }, function(res) {
              if (res.code != 0) {
              }
            })
          };
          //推送
          a.get('Wxpush/CreateOrderPush', {
            order_id: t.info,
            formid: e.data.formid,
            uid: getApp().getCache("userinfo").uid
          }, function(t) {
          })

          if (e.data.options.cart_id) {
            a.get("Cart/DelCart", {
              cart_id: e.data.options.cart_id
            }, function(i) {
              if (i.code != 0) {
              } else {
              }
            })
          };

          e.setData({
            submit: false
          });

          if (e.data.send_type != 'endpay') {
            wx.redirectTo({
              url: "/yb_mingpian/pages/order/pay/index?id=" + t.info
            })
          } else {
            wx.redirectTo({
              url: "/yb_mingpian/pages/order/detail/index?order_id=" + t.info
            })
          }

        } else {
          wx.hideLoading();
          a.alert(t.msg);
        }
    })

  },
  dataChange: function(t) {
    var e = this.data.data,
      a = this.data.list;
    switch (t.target.id) {
      case "buyer_message": //留言
        e.buyer_message = t.detail.value;
        break;
      case "consigner":
        e.consigner = t.detail.value;
        break;
      case "phone":
        e.phone = t.detail.value;
        break;
    }
    this.setData({
      data: e,
      //list: a
    })
  },
  radioChange: function(e) {
    this.setData({
      data: e,
      //list: a
    })
  },

  url: function(t) {
    var e = a.pdata(t).url;
    wx.redirectTo({
      url: e
    })
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
  },

})